﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using popLib;
using PhotoMgr.src.common;
using System.Windows.Forms;
using System.Data;

namespace PhotoMgr.src.database
{
    public partial class DBManager
    {
        public static ReturnSet Search_Setting(int page, int itemPerPage, Hashtable searchData)
        {
            var entity = new PhotoMgrEntities(GenConnectionString());

            IQueryable<Setting> result = entity.Settings.OrderBy(x => x.SettingID);

            if (searchData.ContainsKey("SettingID"))
            {
                int SettingID = NumberLib.getInt(searchData["SettingID"].ToString());
                result = result.Where(x => x.SettingID == SettingID);
            }

            if (searchData.ContainsKey("Status"))
            {
                string Status = searchData["Status"].ToString();
                result = result.Where(x => x.Status == Status);
            }

            return ProcessPageData(page, result, itemPerPage);
        }

        public static Setting Detail_Setting(int setting_id)
        {
            Setting settingR;
            var entity = new PhotoMgrEntities(GenConnectionString());

            IQueryable<Setting> result = entity.Settings.Where(x => x.SettingID == setting_id);

            if (result.Count() < 1)
            {
                return null;
            }

            if (result.Count() == 1)
            {
                settingR = result.First();
            }
            else
            {
                MessageBox.Show("Có nhiều thể loại trủng trong cơ sở dữ liệu", "Thông báo");
                settingR = result.First();
            }

            return settingR;
        }

        public static ReturnSet Save_Setting(bool isUpdateMode, Setting updatedItem, Setting originalItem)
        {
            using (var entity = new PhotoMgrEntities(GenConnectionString()))
            {
                using (var transaction = entity.BeginTransaction())
                {
                    try
                    {
                        //save Bao Cao BHXH first============================================
                        entity.Settings.Attach(updatedItem);

                        if (isUpdateMode)
                        {
                            entity.ApplyOriginalValues("Settings", originalItem);
                        }
                        else
                        {
                            entity.ObjectStateManager.ChangeObjectState(updatedItem, EntityState.Added);
                        }

                        entity.SaveChanges();
                        transaction.Commit();

                        return new ReturnSet(true, updatedItem);
                    }
                    catch (OptimisticConcurrencyException ocex)
                    {
                        entity.Settings.Detach(updatedItem);
                        transaction.Rollback();
                        Global.WriteLog(ocex, "Save_HanhChanh_LuongCanBans.OptimisticConcurrencyException");
                        return new ReturnSet(false, Global.DBErrorType.RecordOutOfDate, ocex.Message);
                    }
                    catch (UpdateException uex)
                    {
                        entity.Settings.Detach(updatedItem);
                        transaction.Rollback();
                        Global.WriteLog(uex, "Save_HanhChanh_LuongCanBans.UpdateException");
                        Global.WriteLog(uex.InnerException, "Save_KeToan_ChiPhiCongNoNoiTru.UpdateException.InnerException");
                        return new ReturnSet(false, Global.DBErrorType.AddExistedRecord, uex.Message);
                    }
                    catch (Exception ex)
                    {
                        entity.Settings.Detach(updatedItem);
                        transaction.Rollback();
                        Global.WriteLog(ex, "Save_HanhChanh_LuongCanBans.Exception");
                        Global.WriteLog(ex.InnerException, "Save_KeToan_ChiPhiCongNoNoiTru.Exception.InnerException");
                        return new ReturnSet(false, Global.DBErrorType.UnknownError, ex.Message);
                    }
                }
            }
        }

        public static ReturnSet CheckExistedSetting(string SettingName)
        {
            using (var entity = new PhotoMgrEntities(GenConnectionString()))
            {
                String SettingNameX = Global.GetString(SettingName);

                var rs = entity.Settings.Where(x => x.SettingName == SettingNameX);

                if (rs.Count() > 0)
                {
                    Setting item = rs.First();
                    return new ReturnSet(true, item.SettingName);
                }

                return new ReturnSet(false);
            }
        }

        public static void Fill_Setting(ComboBox control, bool showSelectAll)
        {
            List<Setting> result = Fill_Setting();
            
            if (showSelectAll)
            {
                Setting item = new Setting();
                item.SettingID = 0;
                item.SettingName = "Tất cả";
                result.Insert(0, item);
            }

            control.DataSource = result;
            control.DisplayMember = "SettingName";
            control.ValueMember = "SettingID";
        }



        public static List<Setting> Fill_Setting()
        {
            using (var entity = new PhotoMgrEntities(GenConnectionString()))
            {
                return entity.Settings.Where(x => x.Status == "A").ToList();
            }
        }
    }
}
